package com.guanyong.www.maoyanDemo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/12 0012.
 */
public class MaoyanSelect {

    public static void main(String[] args) {
        Connection conn = MySQLConnect.getConnection();

        //数据读取：电影id 名称 票房
        String sql = "SELECT movieid, name,ljzhbox,category  FROM t_movie_copy ";
        PreparedStatement pstmt;
        Map<String,MaoyanData> movieMap = new HashMap<String, MaoyanData>();
        try {
            pstmt = (PreparedStatement)conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();

            while (rs.next()) {
                MaoyanData maoyanData = new MaoyanData();
                maoyanData.movieid=rs.getString(1);
                maoyanData.movieName =rs.getString(2);
                maoyanData.ljzhbox=rs.getFloat(3);
                String typeStr=rs.getString(4).split("-->/")[1];
                if (typeStr.indexOf(",")>0){
                        String[] temp=typeStr.split(",");
                        maoyanData.type1Nname=temp[0];
                        maoyanData.type2Nname=temp[1];
                        maoyanData.type1=MaoyanData.getTypeName(temp[0]);
                        maoyanData.type2=MaoyanData.getTypeName(temp[1]);
                }else {
                    maoyanData.type1Nname=typeStr;
                    maoyanData.type2Nname=typeStr;
                    maoyanData.type1=MaoyanData.getTypeName(typeStr);
                    maoyanData.type2=MaoyanData.getTypeName(typeStr);
                }





                movieMap.put(rs.getString(1), maoyanData );
            }


            //数据读取：导演 演员
            String sql1 = "SELECT movieid, titlename,username,celebrityid " +
                    "FROM t_celebrity_copy   " +
                    "WHERE titlename='导演' or titlename='演员' " +
                    "ORDER BY id" ;
            pstmt = (PreparedStatement)conn.prepareStatement(sql1);
            rs = pstmt.executeQuery();
            col = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                if (movieMap.get(rs.getString(1))!=null){
                        if ("导演".equals(rs.getString(2))){
                            if (movieMap.get(rs.getString(1)).daoyan==null){
                                movieMap.get(rs.getString(1)).daoyan=rs.getString(3);
                                movieMap.get(rs.getString(1)).daoyanId=rs.getInt(4);
                            }
                        }else{
                            if (movieMap.get(rs.getString(1)).yanyuan==null){
                                movieMap.get(rs.getString(1)).yanyuan=rs.getString(3);
                                movieMap.get(rs.getString(1)).yanyuanId=rs.getInt(4);
                            }else if (movieMap.get(rs.getString(1)).yanyuan2==null){
                                movieMap.get(rs.getString(1)).yanyuan2=rs.getString(3);
                                movieMap.get(rs.getString(1)).yanyuan2Id=rs.getInt(4);
                            }
                        }
                }
            }

            //数据读取：类型1 类型2
            String sql2 = "SELECT * FROM t_huaxiang   " ;
            pstmt = (PreparedStatement)conn.prepareStatement(sql2);
            rs = pstmt.executeQuery();
            col = rs.getMetaData().getColumnCount();
            float temp1=0.0f;
            float temp2=0.0f;
            int index1=-1;
            int index2=-1;
            while (rs.next()) {
                for (int i = 20; i <= 45; i++) {
                    if (rs.getString(i)!=null){
                        float f= Float.parseFloat(rs.getString(i).substring(0, rs.getString(i).length()-1));
                        if (f>temp1){
                            temp2=temp1;
                            temp1=f;
                            index2=index1;
                            index1=i;
                        }else if (f>temp2){
                            temp2=f;
                            index2=i;
                        }
                    }
                }
                if (movieMap.get(rs.getString(2))!=null){
                    movieMap.get(rs.getString(2)).type1=index1;
                    movieMap.get(rs.getString(2)).type2=index2;
                }

                temp1=0.0f;
                temp2=0.0f;
                index1=-1;
                index2=-1;
            }

            //数据读取：制作 出品
            String sql3 = "SELECT movieid, titlename,companyname,companyid " +
                    "FROM t_company_copy   " +
                    "WHERE titlename='出品' or titlename='制作' " +
                    "ORDER BY id" ;
            pstmt = (PreparedStatement)conn.prepareStatement(sql3);
            rs = pstmt.executeQuery();
            col = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                if (movieMap.get(rs.getString(1))!=null){
                    if ("制作".equals(rs.getString(2))){
                        if (movieMap.get(rs.getString(1)).zhizuo==null){
                            movieMap.get(rs.getString(1)).zhizuo=rs.getString(3);
                            movieMap.get(rs.getString(1)).zhizuoId=rs.getInt(4);
                        }
                    }else{
                        if (movieMap.get(rs.getString(1)).chupin==null){
                            movieMap.get(rs.getString(1)).chupin=rs.getString(3);
                            movieMap.get(rs.getString(1)).chupinId=rs.getInt(4);
                        }
                    }
                }
            }
            for (MaoyanData value : movieMap.values()) {
                if (value.chupin!=null&&value.zhizuo==null){
                    value.zhizuo=value.chupin;
                    value.zhizuoId=value.chupinId;
                }else if (value.zhizuo!=null&&value.chupin==null){
                    value.chupin=value.zhizuo;
                    value.chupinId=value.zhizuoId;
                }
            }

            MySQLConnect.close(rs,pstmt,conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        for (MaoyanData value : movieMap.values()) {
//            if (value.check()){
            if (value.check2()){
                System.out.println("Value = " + value.toString());
                dataTxt.out("E:\\dd.txt", value.toString());
            }
        }



    }

    }
